home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BSETVBUF.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  138 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bsetvbuf.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13.  
  14. /* local headers */
  15. #include "blkio_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      bsetvbuf - assign buffering to a block file
  20.  
  21. SYNOPSIS
  22.      #include <blkio.h>
  23.  
  24.      int bsetvbuf(bp, buf, blksize, bufcnt)
  25.      BLKFILE *bp;
  26.      void *buf;
  27.      size_t blksize;
  28.      size_t bufcnt;
  29.  
  30. DESCRIPTION
  31.      The bsetvbuf function causes the block size and buffer block
  32.      count of the block file associated with BLKFILE pointer bp to be
  33.      changed to bufcnt and blksize, with the same effects as if the
  34.      file had been opened with these values.  If bufcnt has a value of
  35.      zero, the file will be completely unbuffered.  If buf is not the
  36.      NULL pointer, the storage area it points to will be used instead
  37.      of one automatically allocated for buffering.  In this case, buf
  38.      must point to a storage area of size no less than
  39.  
  40.           header size + block size * buffer count
  41.  
  42.      bsetvbuf may be called at any time after opening the block file,
  43.      before and after it is read or written.
  44.  
  45.      bsetvbuf will fail if one or more of the following is true:
  46.  
  47.      [EINVAL]       bp is not a valid BLKFILE pointer.
  48.      [EINVAL]       blksize is less than 1.
  49.      [ENOMEM]       Enough memory is not available for the
  50.                     calling process to allocate.
  51.      [BENOPEN]      bp is not open.
  52.  
  53. SEE ALSO
  54.      bopen, bsetbuf.
  55.  
  56. DIAGNOSTICS
  57.      Upon successful completion, a value of 0 is returned.  Otherwise,
  58.      a value of -1 is returned, and errno set to indicate the error.
  59.  
  60. NOTES
  61.      A common source of error is allocating buffer space as an
  62.      automatic variable in a code block, and then failing to close the
  63.      block file in the same block.
  64.  
  65. ------------------------------------------------------------------------------*/
  66. #ifdef AC_PROTO
  67. int bsetvbuf(BLKFILE *bp, void *buf, size_t blksize, size_t bufcnt)
  68. #else
  69. int bsetvbuf(bp, buf, blksize, bufcnt)
  70. BLKFILE *bp;
  71. void *buf;
  72. size_t blksize;
  73. size_t bufcnt;
  74. #endif
  75. {
  76.     /* validate arguments */
  77.     if (!b_valid(bp) || blksize == 0) {
  78.         errno = EINVAL;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if not open */
  83.     if (!(bp->flags & BIOOPEN)) {
  84.         errno = BENOPEN;
  85.         return -1;
  86.     }
  87.  
  88.     /* synchronize file with buffers */
  89.     if (bsync(bp) == -1) {
  90.         BEPRINT;
  91.         return -1;
  92.     }
  93.  
  94.     /* free old buffer storage */
  95.     b_free(bp);
  96.  
  97.     /* modify BLKFILE control structure contents */
  98.     bp->flags &= ~BIOUSRBUF;
  99.     bp->blksize = blksize;
  100.     bp->bufcnt = bufcnt;
  101.     bp->endblk = 0;
  102.     bp->most = 0;
  103.     bp->least = 0;
  104.     if (b_uendblk(bp, &bp->endblk) == -1) {
  105.         BEPRINT;
  106.         return -1;
  107.     }
  108.  
  109.     /* check if not buffered */
  110.     if (bp->bufcnt == 0) {
  111.         return 0;
  112.     }
  113.  
  114.     /* set user supplied buffer flag */
  115.     if (buf != NULL) {
  116.         bp->flags |= BIOUSRBUF;
  117.     }
  118.  
  119.     /* allocate memory for bp */
  120.     if (b_alloc(bp) == -1) {
  121.         BEPRINT;
  122.         return -1;
  123.     }
  124.  
  125.     /* connect user supplied buffer */
  126.     if (bp->flags & BIOUSRBUF) {
  127.         bp->blkbuf = buf;
  128.     }
  129.  
  130.     /* initialize linked list of buffers */
  131.     if (b_initlist(bp) == -1) {
  132.         BEPRINT;
  133.         return -1;
  134.     }
  135.  
  136.     return 0;
  137. }
  138.